package com.infindo.frame.util;

import java.util.Calendar;
import java.util.Date;
import java.util.TimeZone;


 
public class DateFormat {
    private static Calendar _calendar = Calendar.getInstance();  
	  
    private final static long MSEC_EVERYDAY  = 86400000L;  
    private final static long MSEC_EVERYHOUR = 3600000L;
    private final static long MSEC_EVERYMIN  = 60000L;
    private final static long MSEC_EVERYSEC  = 1000L;
  
    private static int rawOffset = TimeZone.getDefault().getRawOffset(); 
   
	
	public static String millisToDay(long timeMillis)
	{
		int day = toTheDay(timeMillis) ;
		return get2YMD(day);
	}
	
    /**  
     * year month date type 
     *   
     * @param int   the day from 1970-01-01 
     *            
     * @return String year month date type
     */  
    public static String get2YMD(int theDay) {   
        _calendar.setTime(dayToDate(theDay));   
        return  +_calendar.get(Calendar.YEAR) + "-"  
                + (_calendar.get(Calendar.MONTH) + 1 > 9 ? "" : "0")   
                + (_calendar.get(Calendar.MONTH) + 1) + "-"  
                + (_calendar.get(Calendar.DATE) > 9 ? "" : "0")   
                + _calendar.get(Calendar.DATE);   
    } 
	
	public static int getAge(String date)
	{
		int age = 0;
		if(date==null || date.equals(""))
		{
			return 0;
		}
		int index = date.indexOf("-");
		int year = 0;
		@SuppressWarnings("unused")
		int month = 0;
		@SuppressWarnings("unused")
		int day =0;
		String tempdate = date;
		String tempY;
		@SuppressWarnings("unused")
		String tempM;
		@SuppressWarnings("unused")
		String tempD;
		int theDay=toDay();
		if(index!=-1)
		{
			try
			{
				tempY = tempdate.substring(0, index);
				year = Integer.parseInt(tempY);
				if(getYear(theDay)-year<=0)
				{
					return age =0;
				}
				else{
					return getYear(theDay)-year;
				}
//				tempdate = tempdate.substring(index+1);
//				index = tempdate.indexOf("-");
//				if(index!=-1)
//				{
//					tempM = tempdate.substring(0, index);
//					month = Integer.parseInt(tempM);
//					tempD = tempdate.substring(index+1);
//					day = Integer.parseInt(tempD);
//					if(month>getMonth(theDay))
//					{
//						age = getYear(theDay)-year-1;
//					}
//					else if(month<getMonth(theDay))
//					{
//						age = getYear(theDay)-year;
//					}
//					else
//					{
//						 _calendar.setTime(dayToDate(toDay())); 
//						int today =  _calendar.get(Calendar.DATE);  
//						if(day>today)
//						{
//							age = getYear(theDay)-year-1;
//						}
//						else
//							age = getYear(theDay)-year;
//					}
//					return age;
//				}
//				if(month>getMonth(theDay))
//					age = getYear(theDay)-year-1;
//				else
//					age = getYear(theDay)-year;
			}catch(NumberFormatException  ne){}
		}
		return age;
	}
	
	public static int getBirthdayYear(int age){
		return (getYear(toTheDay(getCurrentTime()))-age);
	}
	
	public static long getCurrentTime()
	{
		return System.currentTimeMillis();
	}

    /**  
     * the day  
     *   
     * @param Date  
     *            
     * @return int hthe day from 1970-01-01
     */  
    public static int dateToDay(Date theDate) {   
        return (int) ((theDate.getTime() + rawOffset) / MSEC_EVERYDAY);   
    }   
  
    /**  
     * the date from 1970-01-01
     *   
     * @param int  
     *            
     * @return Date the date from 1970-01-01
     */  
    public static Date dayToDate(int day) {   
        return new Date(day * MSEC_EVERYDAY);   
    }   
  
    /**  
     * today from 1970-01-01 
     *   
     * @return int today from 1970-01-01 
     */  
    public static int toDay() {   
        return (int) ((System.currentTimeMillis() + rawOffset) / MSEC_EVERYDAY);   
    }   
    /**  
     * today from 1970-01-01  
     *   
     * @return int today from 1970-01-01
     */ 
    public static int toTheDay(long timeMillis) {   
        return (int) (timeMillis/MSEC_EVERYDAY);   
    } 
    
    public static int toTheHour(long timeMillis){
    	return (int) ((timeMillis%MSEC_EVERYDAY)/MSEC_EVERYHOUR);
    }
    
    public static int toTheMin(long timeMillis){
    	return (int)(((timeMillis%MSEC_EVERYDAY)%MSEC_EVERYHOUR)/MSEC_EVERYMIN);
    }
    
    public static int toTheSec(long timeMillis){
    	return (int)((((timeMillis%MSEC_EVERYDAY)%MSEC_EVERYHOUR)%MSEC_EVERYMIN)/MSEC_EVERYSEC);
    }
    
    public static String getTime(long timeMillis)
    {
    	String wantTime;
    	int theDay=toTheDay(timeMillis);
    	wantTime=getYear(theDay)+"."+getMD(theDay);
    	return wantTime;
    }
  
    /**  
     * year month date type 
     *   
     * @param int   the day from 1970-01-01 
     *            
     * @return String year month date type
     */  
    public static String getYMD(int theDay) {   
        _calendar.setTime(dayToDate(theDay));   
        return  +_calendar.get(Calendar.YEAR) % 100 + "-"  
                + (_calendar.get(Calendar.MONTH) + 1 > 9 ? "" : "0")   
                + (_calendar.get(Calendar.MONTH) + 1) + "-"  
                + (_calendar.get(Calendar.DATE) > 9 ? "" : "0")   
                + _calendar.get(Calendar.DATE);   
    }   
  
    /**  
     * year month  type 
     *   
     * @param int  the day from 1970-01-01
     *            
     * @return String year month  type
     */  
    public static String getYM(int theDay) {   
        _calendar.setTime(dayToDate(theDay));   
        return _calendar.get(Calendar.YEAR) + "/"  
                + (_calendar.get(Calendar.MONTH) + 1 > 9 ? "" : "0")   
                + (_calendar.get(Calendar.MONTH) + 1);   
    }   
  
    /**  
     * month date type  
     *   
     * @param int  the day from 1970-01-01
     *            
     * @return String month date type 
     */  
    public static String getMD(int theDay) {   
        _calendar.setTime(dayToDate(theDay));   
        return (_calendar.get(Calendar.MONTH) + 1 > 9 ? "" : "0")   
                + (_calendar.get(Calendar.MONTH) + 1) + "."  
                + (_calendar.get(Calendar.DATE) > 9 ? "" : "0")   
                + _calendar.get(Calendar.DATE);   
    }   
  
    /**  
     * the first day of month
     *   
     * @param int  the day from 1970-01-01
     *             
     * @return int the first day of month 
     */  
    public static int getMonthFirstDay(int theDay) {   
        _calendar.setTime(dayToDate(theDay));   
        _calendar.set(Calendar.DAY_OF_MONTH, 1);   
        return (int) (dateToDay(_calendar.getTime()));   
    }   
  
    /**  
     * year
     *   
     * @param int  the day from 1970-01-01
     *             
     * @return int year
     */  
    public static int getYear(int theDay) {   
        _calendar.setTime(dayToDate(theDay));   
        return _calendar.get(Calendar.YEAR);   
    }   
  
    /**  
     * month
     *   
     * @param int  the day from 1970-01-01
     *          
     * @return int month 
     */  
    public static int getMonth(int theDay) {   
        _calendar.setTime(dayToDate(theDay));   
        return _calendar.get(Calendar.MONTH)+1;   
    }   
  
    /**  
     * week 
     *   
     * @param int  the day from 1970-01-01
     *            
     * @return int week
     */  
    public static int getWeek(int theDay) {   
        // 1971-01-03 sunday 
        _calendar.setTime(dayToDate(theDay));   
        return (int) ((_calendar.getTime().getTime() - 172800000L) / 604800000L);   
    } 
}
